﻿using ProTrack.Entity;
using ProTrack.IRepository;
using ProTrack.IServices.Examine;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace ProTrack.Services.Examine
{
    public class QcProjectDeviceSservices : IQcProjectDeviceServices
    {

        //异步方法
        public QcProjectDeviceSservices(IBaseRepository db)
        {
            Db = db;
        }

        public IBaseRepository Db { get; }


        /// <summary>
        /// 插入单条数据
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<bool> AddAsync(QcProjectDevice model)
        {
            var result = await Db.InsertAsync(model);
            return result;
        }

        /// <summary>
        /// 批量删除数据
        /// </summary>
        /// <param name="Ids"></param>
        /// <returns></returns>
        public async Task<bool> DeleteAsync(int[] Ids)
        {
            string sql = "delete QcProjectDevice where Id in @Ids";
            var result = await Db.DeleteAsync(sql, new { Ids });
            return result;
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public async Task<bool> DeleteAsync(int Id)
        {
            string sql = "delete QcProjectDevice where Id = @Id";
            var result = await Db.DeleteAsync(sql, new { Id });
            return result;
        }


        /// <summary>
        /// 根据条件查询数据  分页
        /// </summary>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <param name="DeviceCode"></param>
        /// <param name="DeviceName"></param>
        /// <returns></returns>
        public async Task<IEnumerable<QcProjectDevice>> GetAsync(int PageIndex, int PageSize, string DeviceCode, string DeviceName)
        {
            string sql = "select * from QcProjectDevice where 1=1 ";
            if (!string.IsNullOrEmpty(DeviceCode))
            {
                sql += " and DeviceCode like @DeviceCode ";
            }

            if (!string.IsNullOrEmpty(DeviceName))
            {
                sql += " and DeviceName like @DeviceName ";
            }

            sql += " order by CreateTime desc offset @PageSize*(@PageIndex-1) row fetch next @PageSize rows only ";


            var list = await Db.QueryAsync<QcProjectDevice>(sql, new { PageIndex, PageSize, DeviceCode = $"%{DeviceCode}%", DeviceName = $"%{DeviceName}%" });
            return list;
        }

        /// <summary>
        /// 查询所有数据
        /// </summary>
        /// <returns></returns>
        public async Task<IEnumerable<QcProjectDevice>> GetAllAsync()
        {
            string sql = "select * from QcProjectDevice";
            var list = await Db.QueryAllAsync<QcProjectDevice>(sql);
            return list;
        }


        /// <summary>
        /// 事物修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<bool> UpdateAsync(QcProjectDevice model)
        {
            var result = await Db.UpdateAsync(model);
            return result;
        }

        public async Task<int> GetCountAsync(string DeviceCode, string DeviceName)
        {
            string sql = "select count(*) from QcProjectDevice where 1=1 ";
            if (!string.IsNullOrEmpty(DeviceCode))
            {
                sql += " and DeviceCode like @DeviceCode ";
            }
            if (!string.IsNullOrEmpty(DeviceName))
            {
                sql += " and DeviceName like @DeviceName ";
            }
            var count = await Db.GetRecoreCountAsync(sql, new { DeviceCode = $"%{DeviceCode}%", DeviceName = $"%{DeviceName}%" });
            return count;


        }
    }
}
